#ifndef _STU_C_
#define _STU_C_
#include "stu.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

//--学生信息部分：

void print_wel1() {
	printf("\n++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("\n++\t 欢迎使用学生选课管理系统\t  ++\n");
	printf("\n+++++++++++++++++++++++++++++++++++++++++++++\n");
}

//录入学生子函数（从键盘读取）
void keyboard_stu(void) {
	int i;
	char flag;
	struct student *p1,*p2;
	N2=0;
	p1=p2=(struct student *)malloc(sizeof(struct student));
	printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("\n请依次输入信息（以tab间隔）\n");
	printf("学生学号\t学生姓名\n");
	while(1) {
		scanf("%d\t%s",&p1->stu_id,p1->stu_name);
		p1->scoresum=0;
		p1->nelen=0;
		for(i=0; i<20; i++) {
			p1->nelenum[i]=0;
		}
		if(!N2)	head2=p1;
		p2->next=p1;
		FILE *fp1;
		time_t timep;
		struct tm *p;
		time(&timep);
		p=gmtime(&timep);
		fp1=fopen("log.log","a");
		fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t从键盘输入了一条学号为%d的学生信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec,p1->stu_id);
		fclose(fp1);
		printf("\n录入成功，继续录入输入y，返回上一级请按任意键：");
		while ( (flag = getchar()) != '\n' && flag != EOF ) ; 
		scanf("%c",&flag);
		if(flag!='y'&&'Y') break;
		N2=N2+1;
		p2=p1;
		p1=(struct student*)malloc(sizeof(struct student));
	}
	p1->next=NULL;
	input_stu();
}

//录入学生子函数（从文件读取）
void file_stu(void) {
	int i=0,j,k=0;
	char filepath[50];
	struct student *p1,*p2;
	N2=0;
start:
	;
	FILE *fp;
	memset(filepath,0,sizeof(filepath));
	printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("输入想要读入的文件路径：\n");
	if(k==0)
		getchar();
	gets(filepath);
	if((fp=fopen(filepath,"rb"))==NULL) {
		printf("找不到%s文件！\n",filepath);
		k++;
		fclose(fp);
		goto start;
	}
	p1=p2=(struct student*)malloc(sizeof(struct student));
	fscanf(fp,"%d%s%d%d",&p1->stu_id,p1->stu_name,&p1->nelen,&p1->scoresum);
	for(i=0; i<20; i++) {
		p1->nelenum[i]=0;
	}
	head2=NULL;
	while(!feof(fp)) {
		N2=N2+1;
		if(N2==1)
			head2=p1;
		else
			p2->next=p1;
		p2=p1;
		p1=(struct student*)malloc(sizeof(struct student));
		fscanf(fp,"%d%s%d%d",&p1->stu_id,p1->stu_name,&p1->nelen,&p1->scoresum);
		i++;
		for(i=0; i<20; i++) {
			p1->nelenum[i]=0;
		}
	}
	p1->nelenum[i]=0;
	p2->next=NULL;
	FILE *fp1;
	time_t timep;
	struct tm *p;
	time(&timep);
	p=gmtime(&timep);
	fp1=fopen("log.log","a");
	fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t导入了student.txt文件中的学生信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec);
	fclose(fp1);
	show_stu();
}

//录入学生主函数
void input_stu(void) {
	int i;
	print_wel();
	printf("\t录入学生信息\n");
	printf("\t1. 从键盘录入\n");
	printf("\t2. 从文件录入\n");
	printf("\t3. 浏览学生信息\n");
	printf("\t4. 返回上一级菜单\n");
	printf("\t请选择（1~4): ");
	scanf("%d",&i);
	switch(i) {
		case (1):
			keyboard_stu();
			input_stu();
			break;
		case (2):
			file_stu();
			input_stu();
			break;
		case (3):
			show_stu();
			break;
		case(4):
			manage_stu();
			break;
		default:
			printf("输入有误，请重新输入：\n\n");
			input_cou(); 
			break;
	}
}

//浏览学生信息
void show_stu(void) {
	struct student *q;
	q=head2;
	printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("学生学号\t学生姓名\t已选课程数量\t已选课程学分\n");
	while (q!=NULL) {
		printf("%6d%14s%14d%14d\n",q->stu_id,q->stu_name,q->nelen,q->scoresum);
		q=q->next;
	}
	FILE *fp1;
	time_t timep;
	struct tm *p;
	time(&timep);
	p=gmtime(&timep);
	fp1=fopen("log.log","a");
	fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t浏览了学生信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec);
	fclose(fp1);
}


//学生管理子函数（修改学生）
void insert_stu(int num3) {
	int i;
	struct student *p1,*p2;
	printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	if(head2==NULL) {
		printf("\n\t没有该学生信息,无法修改!\n");
		goto end;
	}
	p1=head2;
	while(num3!=p1->stu_id && p1->next!=NULL) {
		p2=p1;
		p1=p1->next;
	}
	if(num3==p1->stu_id) {
		printf("\t重新修改数据(用tab间隔)\n");
		printf("学生学号 学生姓名 总学分\n");
		scanf("%d\t%s\t%d",&p1->stu_id,p1->stu_name,&p1->scoresum);
		for(i=0; i<20; i++) {
			p1->nelenum[i]=0;
		}
		printf("\t修改完毕\n");
		FILE *fp1;
		time_t timep;
		struct tm *p;
		time(&timep);
		p=gmtime(&timep);
		fp1=fopen("log.log","a");
		fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t修改了一条学号为%d学生信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec,num3);
		fclose(fp1);
	} else
		printf("\t无该学号的学生！\n");
end:
	;
}

//学生管理（删除学生）
void delete_stu(int num) {
	struct student *p1,*p2;
	if(head2==NULL) {
		printf("\n\t没有该学生信息,无法删除!\n");
		goto end;
	}
	p1=head2;
	while(num!=p1->stu_id && p1->next != NULL) {
		p2=p1;
		p1=p1->next;
	}
	if(num==p1->stu_id) {
		if(p1==head2)
			head2=p1->next;
		else
			p2->next=p1->next;
		printf("\t已删除该学生信息！\n");
		FILE *fp1;
		time_t timep;
		struct tm *p;
		time(&timep);
		p=gmtime(&timep);
		fp1=fopen("log.log","a");
		fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t删除了一条学号为%d学生信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec,num);
		fclose(fp1);
		N2=N2-1;
	} else
		printf("\t无该学号的学生！\n");
end:
	;
}

//按学号查找 学生
void search_stu_id(void) {
	int a,b;
	struct student *p;
	printf("\t请输入学生学号：");
	scanf("%d",&a);
	p=head2;
	while(p!=NULL) {
		if(p->stu_id==a) {
			printf("学号：%6d\n",p->stu_id);
			printf("姓名：%6s\n",p->stu_name);
			printf("所选课程编号及名称：\n");
			for(b=0; p->nelenum[b]>0; b++) {
				printf("%5d %10s\n",p->nelenum[b],p->nelename[b-1]);
			}
			printf("所选课程总学分：%d\n",p->scoresum);
		}
		p=p->next;
	}
	FILE *fp1;
	time_t timep;
	struct tm *p1;
	time(&timep);
	p1=gmtime(&timep);
	fp1=fopen("log.log","a");
	fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t查找了一条学号为%d学生信息。\n",1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday,8+p1->tm_hour,p1->tm_min,p1->tm_sec,a);
	fclose(fp1);
}

//按姓名查找 学生
void search_stu_name(void) {
	int b;
	char name[50];
	struct student *p;
	printf("\t请输入学生姓名：");
	scanf("%s",name);
	p=head2;
	while(p!=NULL) {
		if(strcmp(name,p->stu_name)==0) {
			printf("学号：%6d\n",p->stu_id);
			printf("姓名：%6s\n",p->stu_name);
			printf("所选课程编号及名称：\n");
			for(b=0; p->nelenum[b]>0; b++) {
				printf("%5d %10s\n",p->nelenum[b],p->nelename[b-1]);
			}
			printf("所选课程总学分：%d\n",p->scoresum);
		}
		p=p->next;
	}
	FILE *fp1;
	time_t timep;
	struct tm *p1;
	time(&timep);
	p1=gmtime(&timep);
	fp1=fopen("log.log","a");
	fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t按姓名查找了一条学号为%d学生信息。\n",1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday,8+p1->tm_hour,p1->tm_min,p1->tm_sec,p->stu_id);
	fclose(fp1);
}

//学生信息查找主函数
void search_stu(void) {
	int i;
	print_wel();
	printf("\t学生信息查找\n");
	printf("\t1. 按学号查找\n");
	printf("\t2. 按姓名查找\n");
	printf("\t3. 返回上一级目录\n");
	printf("\t请输入(1~3)：");
	scanf("%d",&i);
	switch(i) {
		case (1):
			search_stu_id();
			search_stu();
			break;
		case (2):
			search_stu_name();
			search_stu();
			break;
		case (3):
			manage_stu();
			break;
	}
}

void into_stu() { //存储学生信息
	FILE *fp;
	struct student *p;
	char filepath[50];
	printf("保存数据路径：\n");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"w"))==NULL) {
		printf("无法存储！\n");
		exit(0);
	}
	p=head2;
	fprintf(fp,"学生学号 姓名 已选课程数目 已选课程总计学分\n");
	while(p!=NULL) {
		fprintf(fp,"%d\t%s\t%d\t%d\n",p->stu_id,p->stu_name,p->nelen,p->scoresum);
		p = p->next;
	}
	fclose(fp);
	printf("已储存入%s文件！\n",filepath);
}


//学生管理主函数
void manage_stu(void) {
	int i,num2,num3;
	print_wel1();
	printf("\t学生信息管理\n");
	printf("\t1. 添加学生信息\n");
	printf("\t2. 修改学生信息\n");
	printf("\t3. 删除学生信息\n");
	printf("\t4. 存储学生信息\n");
	printf("\t5. 查找学生信息\n");
	printf("\t6. 浏览学生信息\n");
	printf("\t7. 返回上一级菜单\n");
	printf("\t请选择(1~7): ");
	scanf("%d",&i);
	switch(i) {
		case(1) :
			print_wel1();
			input_stu();
			
			break;
		case(2) : {
			
			print_wel1();
			printf("请输入要修改的学生学号： ");
			scanf("%d",&num3);
			insert_stu(num3);
			manage_stu();
			break;
		}
		case(3) : {
			
			print_wel1();
			printf("请输入要删除的学生学号： ");
			scanf("%d",&num2);
			delete_stu(num2);
			manage_stu();
			break;
		}
		case(4) :
			
			into_stu();
			manage_stu();
			break;
		case(5) :
			print_wel();
			search_stu();
			manage_stu();
			break;
		case(6) :
			
			show_stu();
			manage_stu();
			break;
		case(7) :
			
			break;
		default:
			printf("输入有误，请重新输入：\n\n");
			manage_stu(); 
			break;
	}
}
#endif
